-- half build consume
AddComponentPostInit("builder", function(Builder)
    local oldRemoveIngredients = Builder.RemoveIngredients
    function Builder:RemoveIngredients(ingredients, recname, discounted,...)
        local res = {oldRemoveIngredients(self, ingredients, recname, discounted,...)}
        self.inst.components.builder.ingredientmod = self.inst.ymh_orig_ingredientmod or 1

        local hat = self.inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
        if hat and hat.prefab and hat.prefab == 'yuemiheng_hat' then
            if hat.components.finiteuses then
                hat.components.finiteuses:Use(TUNING.MOD_YUEMIHENG.HAT.HALF_BUILD_CONSUME_USE)
            end
        end
     
        return unpack(res)
    end

    -- half consume
    local oldGetIngredients = Builder.GetIngredients
    function Builder:GetIngredients(...)
        if self.inst.components.inventory and self.inst.components.inventory:EquipHasTag('yuemiheng_hat') then
            self.inst.ymh_orig_ingredientmod = self.inst.components.builder.ingredientmod
            self.inst.components.builder.ingredientmod = .5
        end
        return oldGetIngredients(self, ...)
    end
end)


-- 80%免伤 每十秒获得一次免伤，

AddComponentPostInit('combat',function (self)
    local old_GetAttacked = self.GetAttacked
    function self:GetAttacked(attacker,damage,weapon,stimuli,spdamage,...)
        if self.inst:HasTag("player") then
            local hat = self.inst.components.inventory and self.inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
            if hat and hat.prefab and hat.prefab == 'yuemiheng_hat' then
                if damage then
                    damage = damage * (1-TUNING.MOD_YUEMIHENG.HAT.ABSORB)
                end

                if hat.components.rechargeable and hat.components.rechargeable:IsCharged() then
                    if damage then
                        damage = 0
                    end
                    hat.components.rechargeable:Discharge(TUNING.MOD_YUEMIHENG.HAT.AVOID_DMG_INTERVAL)
                    if hat.components.finiteuses then
                        hat.components.finiteuses:Use(TUNING.MOD_YUEMIHENG.HAT.AVOID_DMG_USE)
                    end
                end
            end
        end
        return old_GetAttacked(self,attacker,damage,weapon,stimuli,spdamage,...)
    end
end)
